#!/bin/bash

# Post-install script for the full Lemonade package (with Electron app)

INSTALL_DIR="/usr/local/share/lemonade-server"
LLAMA_DIR="${INSTALL_DIR}/llama"
APP_DIR="${INSTALL_DIR}/app"

# Create llama directory if it doesn't exist and make it writable
mkdir -p "$LLAMA_DIR"
chmod 777 "$LLAMA_DIR"

# Make the Electron app executable
if [ -f "$APP_DIR/lemonade" ]; then
    chmod +x "$APP_DIR/lemonade"
fi

# Fix chrome-sandbox permissions (required for Electron/Chromium sandbox)
# The chrome-sandbox binary must be owned by root and have SUID bit set
if [ -f "$APP_DIR/chrome-sandbox" ]; then
    chown root:root "$APP_DIR/chrome-sandbox"
    chmod 4755 "$APP_DIR/chrome-sandbox"
fi

# Make other Electron helper binaries executable
for binary in "$APP_DIR/chrome_crashpad_handler" "$APP_DIR/libvulkan.so.1" "$APP_DIR/libvk_swiftshader.so" "$APP_DIR/libGLESv2.so" "$APP_DIR/libEGL.so"; do
    if [ -f "$binary" ]; then
        chmod +x "$binary"
    fi
done

# Ensure all .so files in the app directory are readable and executable
find "$APP_DIR" -name "*.so*" -type f -exec chmod 755 {} \; 2>/dev/null || true

exit 0

